package org.jin.samples.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.jin.samples.dao.IScoreDao;
import org.jin.samples.dao.ISubjectDao;
import org.jin.samples.dao.IUserSubjectDAO;
import org.jin.samples.model.CheckUser;
import org.jin.samples.model.ExamScore;
import org.jin.samples.model.ExamSubject;
import org.jin.samples.model.ExamUser;
import org.jin.samples.model.ExamUserSubject;
import org.jin.samples.model.vo.ExamScoreVO;
import org.jin.samples.model.vo.ExamSubjectVO;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.model.vo.UserSubjectScoreVO;
import org.jin.samples.page.PagerModel;
import org.jin.samples.service.IScoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ScoreServiceImpl implements IScoreService{
	
	@Autowired
	private IScoreDao scoreDao;
	
	@Autowired
	private IUserSubjectDAO userSubjectDAO;
	
	@Autowired
	private ISubjectDao subjectDao;
	

	@Override
	public List<ExamScore> findAllByScoreId(String userId) {
		List<ExamScore> scoreList = scoreDao.findAllByScoreId(userId);
		return scoreList;
	}
	
	/*@Override
	public ExamSubject findAllBySubjectId(Integer subjectId){
		PagerModel<ExamUserSubject> listUS = userSubjectDAO.findAllSubjectByUserIdPage(subjectId);
		PagerModel<ExamScore> scoreList = scoreDao.findAllScoreByPage(subjectId);
		
		
		return subjectDao.findAllBySubjectId(subjectId);
	}*/
	
	@Override
	public PagerModel<UserSubjectScoreVO> findAllSubjectScore(String id)throws Exception{
		PagerModel<ExamUserSubject> listUS = userSubjectDAO.findAllSubjectByUserIdPage(id);
		PagerModel<ExamScore> scoreList = scoreDao.findAllScoreByPage(id);
		PagerModel<UserSubjectScoreVO> ussVOPageList = new PagerModel<UserSubjectScoreVO>();
		List<UserSubjectScoreVO> ussVOList = new ArrayList<UserSubjectScoreVO>();
		for(ExamUserSubject us : listUS.getDates()){
			Integer subId = us.getSubject().getSubId();
			UserSubjectScoreVO ussVO = new UserSubjectScoreVO();
			for(ExamScore s : scoreList.getDates()){
				ExamScore score = new ExamScore();
				if(subId.equals(s.getSubjectId().getSubId())){
					ussVO.setScore(s);
					break;
				}else{
					ussVO.setScore(score);
				}
			}
			ussVO.setSubject(us.getSubject());
			ussVO.setUser(us.getUser());
			ussVOList.add(ussVO);
			ussVOPageList.setOffset(scoreList.getOffset());
			ussVOPageList.setPageSize(scoreList.getPageSize());
			ussVOPageList.setTotalSize(scoreList.getTotalSize());
			ussVOPageList.setDates(ussVOList);
		}
		return ussVOPageList;	
	}
	
	@Override
	public ExamScoreVO findScoreByUSId(String userId, Integer subjectId)throws Exception{
		ExamScoreVO scoreVO = new ExamScoreVO();
		ExamScore score = scoreDao.findScoreByUSId(userId, subjectId);
		BeanUtils.copyProperties(score, scoreVO);
		
		ExamUserVO userVO = new ExamUserVO();
		userVO.setUserId(score.getUserId().getUserId());
		userVO.setUserName(score.getUserId().getUserName());
		//BeanUtils.copyProperties(score.getUserId().get, userVO);
		scoreVO.setUserId(userVO);
		
		ExamSubjectVO subjectVO = new ExamSubjectVO();
		BeanUtils.copyProperties(score.getSubjectId(),subjectVO);
		scoreVO.setSubjectId(subjectVO);
		
		return scoreVO;	
	}
	
	@Override
	public void addScore(ExamScoreVO scoreVO) throws Exception {
		System.out.println("***********addScore***********");
		System.out.println("scoreVO.getId()="+scoreVO.getSubjectId().getSubId());
		ExamScore score = scoreDao.get(scoreVO.getId());
		score.setScore(scoreVO.getScore());
		scoreDao.save(score);
		
		if(score.getScore() != null){
			ExamSubject es = subjectDao.get(scoreVO.getSubjectId().getSubId());
			es.setStatus(0);
			if(es != null){
				subjectDao.update(es);
			}
		}
	}
	
	@Override
	public List<ExamScore> checkUser(CheckUser checkUser) throws Exception {
		return scoreDao.checkUser(checkUser);
	}

	
}
